package cn.cc.goods.service.serviceImpl;

import cn.cc.common.beanCopy.utils.BeanCopierUtil;
import cn.cc.common.redis.RedisCommon;
import cn.cc.common.result.utils.RestResult;
import cn.cc.common.result.utils.RestResultEmum;
import cn.cc.goods.mapper.mapper.SmallMiGoodsTypeMapper;
import cn.cc.goods.mapper.pojo.SmallMiGoodsType;
import cn.cc.goods.service.GoodsTypeService;
import cn.cc.goods.service.bo.AddGoodsTypeBO;
import cn.cc.goods.service.bo.DelGoodsTypeBO;
import cn.cc.goods.service.bo.UpdGoodsTypeBO;
import cn.cc.goods.service.domain.GetGoodsTypeDO;
import cn.cc.goods.service.domain.GetTypeByTypeDO;
import cn.hutool.core.lang.generator.ObjectIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @program: smail-mi-cloud
 * @description: 商品类型   service
 * @author: Mr.WuChao
 * @create: 2021-07-16 14:33
 **/
@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {
    @Autowired
    private SmallMiGoodsTypeMapper typeMapper;
    @Autowired
    private RedisCommon redisCommon;

    @Override
    public RestResult<Void> addGoodsType(AddGoodsTypeBO typeBO) {
        //判断数据是否真实
        //将数据存入redis
        SmallMiGoodsType goodsType = BeanCopierUtil.copyObj(typeBO, SmallMiGoodsType.class);
        goodsType.setCreationDate(new Date());
        goodsType.setModifyDate(new Date());
        //自动生成id
        ObjectIdGenerator objectIdGenerator = new ObjectIdGenerator();
        String id = objectIdGenerator.next();
        goodsType.setGoodsTypeId(id);
        goodsType.setVersion("1.0");
        goodsType.setCreatedBy("cc");
        int i = typeMapper.insertSelective(goodsType);
        if (i == 1) {
            return new RestResult<>(RestResultEmum.SUCCESS.code, RestResultEmum.SUCCESS.msg);
        }
        return new RestResult<>(RestResultEmum.SAVE_ERROR.code, RestResultEmum.SAVE_ERROR.msg);
    }

    @Override
    public RestResult<List<GetGoodsTypeDO>> getGoodsTypeAll() {
        List<SmallMiGoodsType> types = typeMapper.selectAll();
        List<GetGoodsTypeDO> typeDOS = BeanCopierUtil.copyList(types, GetGoodsTypeDO.class);
        if (typeDOS == null || typeDOS.size() == 0) {
            return new RestResult<>(RestResultEmum.GET_ERROR.code, RestResultEmum.GET_ERROR.msg);
        }
        return new RestResult<>(typeDOS);
    }

    @Override
    public RestResult<Void> delGoodsType(DelGoodsTypeBO delGoodsTypeBO) {
        SmallMiGoodsType goodsType = BeanCopierUtil.copyObj(delGoodsTypeBO, SmallMiGoodsType.class);
        int delete = typeMapper.delete(goodsType);
        if (delete == 0) {
            return new RestResult<>(RestResultEmum.DEL_ERROR.code, RestResultEmum.DEL_ERROR.msg);
        }
        return new RestResult<>();
    }

    @Override
    public RestResult<Void> updGoodsType(UpdGoodsTypeBO updGoodsTypeBO) {
        SmallMiGoodsType goodsType = BeanCopierUtil.copyObj(updGoodsTypeBO, SmallMiGoodsType.class);
        int update = typeMapper.updateByPrimaryKeySelective(goodsType);
        if (update == 0) {
            return new RestResult<>(RestResultEmum.EDIT_ERROR.code, RestResultEmum.EDIT_ERROR.msg);
        }
        return new RestResult<>();
    }

    @Override
    public RestResult<Void> addGoodsTypeOne(AddGoodsTypeBO addGoodsTypeBO) {
        SmallMiGoodsType goodsType = BeanCopierUtil.copyObj(addGoodsTypeBO, SmallMiGoodsType.class);
        int insert = typeMapper.insertSelective(goodsType);
        if (insert == 0) {
            return new RestResult<>(RestResultEmum.SAVE_ERROR.code, RestResultEmum.SAVE_ERROR.msg);
        }
        return new RestResult<>();
    }
}
